package com.fingard.dsp.bank.directbank.gxnx01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/10/10.
 */
public class GXNX01Balance extends GXNX01Base implements IBalance{

    /**
     * 实时余额查询
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        return d_7506_BalQryA(p_req7506);
    }

    /**
     * 公网线路-余额查询
     * @param p_req7506
     * @return
     * @throws Exception
     */
    public RetAts7506BalQry d_7506_BalQryA(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try {
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = null;
                //签名原文
                String signDataStr=bankFront.userName+"|"+p_req7506.readedDetail.actNum+"|"+bankFront.org;
                //获取签名后的数据
                String signData=sendBankBySigned(signDataStr);
                if("".equals(signData)){
                    detail = new RetAtsBalDetail();
                    detail.respCode = "-1";
                    detail.respInfo = "获取签名后的数据失败，请求报文发送失败";
                }else {
                    //非公有参数
                    XmlTextWriter xmlTextWriter = new XmlTextWriter();
                    GXNX01Header header = new GXNX01Header();
                    header.tranCode ="CBE002";
                    writeCommonHeader(xmlTextWriter,header);
                    xmlTextWriter.writeStartElement("ReqParam");
                    xmlTextWriter.writeElementString("accountNo",p_req7506.readedDetail.actNum);
                    xmlTextWriter.writeElementString("currencyType",convertCurToBank(p_req7506.readedDetail.actCur));
                    xmlTextWriter.writeElementString("branchId",bankFront.org);//机构号
                    xmlTextWriter.writeElementString("signDataStr",signDataStr.trim());//签名原文
                    xmlTextWriter.writeElementString("signData",signData.trim());//签名后数据
                    xmlTextWriter.writeEndElement();
                    xmlTextWriter.writeEndDocument();
                    String[] bankResult = sendToBank(xmlTextWriter.toXmlString());

                    //测试
                    //bankResult[0]="";
                    //bankResult[1]="0000||#<?xml version=\"1.0\" encoding=\"utf-8\"?><QLBankData>  <opRep>    <opResult>      <customerHostNo>1111111111</customerHostNo>      <customerNameCN>哈哈</customerNameCN>      <accountOpenNode>11111</accountOpenNode>      <accountOpenBankName>哈哈</accountOpenBankName>      <accountNo>10101111100010</accountNo><accountType>1</accountType><accountState>0</accountState><currencyTyp>CNY</currencyTyp><balance>100.00</balance><balanceAvailable>100.00</balanceAvailable><errorCode></errorCode><errorMessage></errorMessage>    </opResult>  </opRep></QLBankData>\n";
                    //WriteBankLogStep2(bankResult[1]);

                    if (bankResult[0].length() == 0) {
                        String[] result=bankResult[1].split("\\|");
                        StringBuilder tmpSb = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(result[2].substring(1), tmpSb);
                        if (tmpBkReader != null) {
                            String rspCode = result[0];//响应码
                            String rspMsg = result[1];//响应结果
                            if (isActiveRetCode(rspCode)) {
                                if (tmpBkReader.read("/QLBankData/opRep/opResult")){
                                    String errorCode = tmpBkReader.readedXElement.getSingleTextNotNull("errorCode");//错误码
                                    if(StringHelper.isNullOrEmpty(errorCode) || "000000".equals(errorCode)){
                                        detail = new RetAtsBalDetail();
                                        detail.respCode = "0";
                                        detail.respInfo = rspMsg;
                                        detail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("currencyTyp"));
                                        detail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("accountNo");
                                        detail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("balanceAvailable");
                                        detail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("balance");
                                    }else {
                                        detail = new RetAtsBalDetail();
                                        detail.respCode = "-1";
                                        detail.respInfo = tmpBkReader.readedXElement.getSingleTextNotNull("errorMessage");
                                    }
                                }
                            } else {
                                detail = new RetAtsBalDetail();
                                detail.respCode = "-1";
                                detail.respInfo = rspMsg.toString();
                            }
                        } else {
                            detail = new RetAtsBalDetail();
                            detail.respCode = "-1";
                            detail.respInfo = tmpSb.toString();
                        }
                    } else {
                        detail = new RetAtsBalDetail();
                        detail.respCode = "-1";
                        detail.respInfo = bankResult[1];
                    }
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
